import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.io.Files;
public class SendCourses
{
    public static void main(String[] args) throws IOException
	{
		System.setProperty("webdriver.chrome.driver", "D:\\Users\\yunmo\\yunmo\\chromedriver.exe");
        WebDriver driver = new ChromeDriver(new ChromeOptions().addArguments("--headless"));
        driver.manage().timeouts().implicitlyWait(90L, TimeUnit.SECONDS);
        while (true)
        {
        	driver.get("http://jwts.hitwh.edu.cn/loginNOCAS");
	        driver.findElement(By.id("usercode")).sendKeys("180400511");
	        driver.findElement(By.id("password")).sendKeys("217125Lagrange");
	        try {Files.copy(driver.findElement(By.id("yzmmsg")).getScreenshotAs(OutputType.FILE), new File("D:\\Users\\test.jpg"));}catch (IOException e) {continue;}
	        System.out.print("验证码：");
	        driver.findElement(By.id("code")).sendKeys(new String(System.in.readNBytes(4)));
	        System.in.read();
	        driver.findElement(By.xpath("//*[@id=\"loginform\"]/div[2]")).click();
	        try{driver.switchTo().alert().dismiss();}catch (NoAlertPresentException e) {break;}
        }
        new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id=\"tabs_container\"]/div/span[5]/p[1]"))).perform();
        driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div/div[2]/div/span[5]/p[5]/a")).click();
        driver.switchTo().frame("iframename");
//        byte[][] messages = new byte[44][];//1 + 6 * 7 + 1];
        String termCodeName = new WebDriverWait(driver, 60L).until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[1]/div/div[8]/div[1]/span"))).getText();
//        messages[0] = termCodeName.substring(0, termCodeName.length() - "课表(2020-09-01 12:07:52)".length()).getBytes();
        System.out.println(termCodeName);
        List<WebElement> courses = driver.findElements(By.xpath("/html/body/div[1]/div/div[8]/div[2]/table/tbody/tr/td"));
        for (int i = 2;i < courses.size();++i)
        {
        	System.out.println(courses.get(i).getText());
//        	int temp = i % 9;
//        	if (temp != 0 && temp != 1)
//        		messages[i - 1 - (i / 9 << 1)] = courses.get(i).getText().getBytes();
        }
        driver.switchTo().defaultContent();
        driver.findElement(By.id("logout")).click();
        driver.switchTo().alert().accept();
        driver.quit();
	}
}